package com.neusoft.bizcore.activiti.converter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.activiti.dto.ActSerialNumberDTO;
import com.neusoft.bizcore.activiti.model.ActSerialNumber;
import com.neusoft.bizcore.activiti.repository.ActSerialNumberRepository;
import com.neusoft.bizcore.web.dto.AbstractConverter;

@Component
public class ActSerialNumberConverter extends AbstractConverter<ActSerialNumber, ActSerialNumberDTO> {

    @Autowired
    private ActSerialNumberRepository serialNumberRepository;

    @Override
    public ActSerialNumber toModel(final ActSerialNumberDTO dto) {

        ActSerialNumber model;
        if (dto.isNew()) {
            model = new ActSerialNumber();
        } else {
            model = this.serialNumberRepository.getOne(dto.getId());
        }

        model.setConfigTemplet(dto.getConfigTemplet());
        model.setIsAutoIncrement(dto.getIsAutoIncrement());
        model.setMaxSerial(dto.getMaxSerial());
        model.setModuleCode(dto.getModuleCode());
        model.setModuleName(dto.getModuleName());
        model.setPreMaxNum(dto.getPreMaxNum());

        return model;
    }

    @Override
    public ActSerialNumberDTO toDTO(final ActSerialNumber model, final boolean forListView) {

        final ActSerialNumberDTO dto = new ActSerialNumberDTO();

        dto.setId(model.getId());
        dto.setConfigTemplet(model.getConfigTemplet());
        dto.setIsAutoIncrement(model.getIsAutoIncrement());
        dto.setMaxSerial(model.getMaxSerial());
        dto.setModuleCode(model.getModuleCode());
        dto.setModuleName(model.getModuleName());
        dto.setPreMaxNum(model.getPreMaxNum());

        return dto;
    }

}
